<?php namespace system\model;
use houdunwang\model\Model;
class Admin extends Model{
	//数据表
	protected $table = "admin";


	public  function login(){

//      1. 验证表单数据是否为空
		$res = Validate::make( [
			//字段、验证规则、提示消息、验证条件,
			[ 'admin_username', 'isnull', '用户名不能为空', self::MUST_VALIDATE ],
			[ 'admin_password', 'isnull', '密码不能为空', self::MUST_VALIDATE ],
			[ 'code', 'isnull', '验证码不能为空', self::MUST_VALIDATE ],
		] );
		if($res===false){
			print_r(['valid'=>0,'msg'=>Validate::getError()]);
//			print_r(Validate::getError());
		}
//		2.验证表单是否证据
//		echo $encrypted = Crypt::encrypt('admin888'); die;
		$userInfo = $this->where('admin_username',$_POST['admin_username'])->where('admin_password', Crypt::encrypt($_POST['admin_password']))->first();
		if(!$userInfo){
			//用户名密码不对
			return ['valid'=>0,'msg'=>'用户名或密码错误'];
		}

//		3.验证验证码类
		if(strtolower(Code::get())!=strtolower($_POST['code'])){
			return ['valid'=>0,'msg'=>'验证码错误'];
		}
//		p(Session::all());die;
//		p($userInfo);die;
//		4.存session
		Session::set('admin.admin_id',$userInfo['admin_aid']);
		Session::set('admin.admin_name',$userInfo['admin_username']);
//		p(Session::all());die;
		return ['valid'=>1,'msg'=>'登录成功'];
	}



}